package com.gitee.wsl.common.serialization

import androidx.compose.ui.graphics.Color
import com.gitee.wsl.common.ui.ext.color.hexToColor
import com.gitee.wsl.common.ui.ext.color.hexString
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class ColorSerializer : KSerializer<Color> {
    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(Color::class.qualifiedName!!, PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: Color) {
        encoder.encodeString(value.hexString)
    }

    override fun deserialize(decoder: Decoder): Color = decoder.decodeString().hexToColor
}